﻿using UnityEngine;
using System.Collections;

public class scoreKeeping : MonoBehaviour {
	
	public int winningScore = 1; 
	public GUIStyle customGuiStyle;
	private bool gameOver = false;
	private bool gameStarted = false; 

	public static int player1Score  = 0; 
	public static int player2Score  = 0; 
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if (player1Score >= winningScore || player2Score >= winningScore) 
		{
			gameOver = true; 
		}
	 	if (gameOver && Application.loadedLevelName != "GameOver") 
	 	{
	 		Application.LoadLevel("GameOver");
	 	}
	}
	
	void Awake() { 
 		DontDestroyOnLoad (gameObject); 
	} 
	
	void OnGUI()
	{
		GUI.Label (new Rect (0, 0, Screen.width, 100), player1Score +" | "+ player2Score, customGuiStyle); 
	 	if (!gameStarted) 
	 	{ 
		 	if (GUI.Button(new Rect ( (Screen.width/2)-60, (Screen.height/2), 120 , 30), "Two Players")) 
		 	{ 
			 	GameObject.Find("paddle2").GetComponent<paddleAutoControl>().enabled = false; 
			 	GameObject.Find("paddle2").GetComponent<paddleController2>().enabled = true; 
			 	gameStarted = true; 
			 	GameObject.Find("ballLauncher").SendMessage("LaunchBall", 1); 
		 	} 
		 	if (GUI.Button(new Rect ( (Screen.width/2)-60, (Screen.height/2)+40, 120 , 30), "Single Player")) 
		 	{ 
			 	GameObject.Find("paddle2").GetComponent<paddleAutoControl>().enabled = true; 
			 	GameObject.Find("paddle2").GetComponent<paddleController2>().enabled = false; 
			 	gameStarted = true; 
			 	GameObject.Find("ballLauncher").SendMessage("LaunchBall", 2); 
		 	} 
	 	}
	 	if (gameOver) 
	 	{ 
	 		if (player1Score > player2Score) 
	 		{
	 			GUI.Label (new Rect (0, Screen.height/2, Screen.width , 100), "PLAYER ONE WINS!",customGuiStyle); 
	 		}
	 		else 
	 		{
	 			GUI.Label (new Rect (0, Screen.height/2, Screen.width , 100), "PLAYER TWO WINS!",customGuiStyle); 
	 		}
	 		if (GUI.Button(new Rect ( (Screen.width/2)-60, (Screen.height/2)+40, 120 , 60), "New Game")) 
			{ 
				player1Score = 0; 
			 	player2Score = 0; 
			 	gameOver = false; 
			 	Destroy(gameObject); 
			 	Application.LoadLevel(0); 
			} 
	 	} 
	}
}
